package com.aistar.controller;

import com.aistar.model.bo.Result;
import com.aistar.model.entity.*;
import com.aistar.service.job.*;
import com.aistar.service.job.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName: JobRestController
 * @author: LiuWenXiu
 * @date: 2021/9/26 15:47
 * @Description: TODO...本类用来演示:
 */

@RestController
public class JobRestController {
    @Autowired
    private IRecruitmentService recruitmentService;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IRecruitmentLabelService recruitmentLabelService;
    @Autowired
    private ITagService tagService;
    @Autowired
    private IEmployeeCollectionService employeeCollectionService;
    @Autowired
    private IEmployeeBlackListService employeeBlackListService;
    @Autowired
    private IDeliveryService deliveryService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/job/add")
    public Result employeeCollection(HttpServletRequest request, Integer recruitmentId) {
//        Recruitment recruitment = recruitmentService.loadRecruitment(recruitmentId);
//        EmployeeCollection employeeCollection = new EmployeeCollection();
//        employeeCollection.setRecruitmentId(recruitmentId);
//        employeeCollection.setRecruitmentName(recruitment.getName());
//        HttpSession session = request.getSession();
//        Employee employee= (Employee) session.getAttribute("employee");
//        employeeCollection.setEmployeeId(employee.getId());
//        int count=employeeCollectionService.addRecruitment(employeeCollection,recruitmentId,employee.getId());
//        if(count==0){
//            return new Result("500","已收藏该公司!");
//        }
        //首先判断是否可以进行收藏操作
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
        //判断是否已经被收藏
        boolean flag=employeeCollectionService.judgeCollection(employee.getId(),recruitmentId);
        if(flag){
            return new Result("500","抱歉,已收藏");
        }else{
            //如果没有收藏,先保存一份到redis中
            employeeCollectionService.saveToRedis(recruitmentId,employee.getId());
            employeeCollectionService.saveToMySql(recruitmentId,employee.getId());
            return new Result("200", "收藏成功");
        }
    }


    @PostMapping("/job/detel")
    public Result employeeBlackList(HttpServletRequest request, Integer recruitmentId) {
//        Company company = companyService.findById(companyId);
//        EmployeeBlacklist employeeBlacklist = new EmployeeBlacklist();
//        employeeBlacklist.setCompanyId(company.getId());
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
//        employeeBlacklist.setEmployeeId(employee.getId());
//        int count = employeeBlackListService.saveBlackList(employeeBlacklist,companyId,employee.getId());
//        if(count==0){
//            return new Result("500","已屏蔽该公司!");
//        }
        boolean flag = employeeBlackListService.judgeBlacklist(employee.getId(),recruitmentId);
        if(flag){
            return new Result("500","抱歉,已屏蔽");
        }else {
            employeeBlackListService.saveToRedis(recruitmentId,employee.getId());
            employeeBlackListService.saveToMySql(recruitmentId,employee.getId());
            return new Result("200", "屏蔽成功");
        }

    }

    @PostMapping("/job/send")
    public Result delivery(HttpServletRequest request, Integer resumeId, Integer recruitmentId){
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
        Integer employeeId = employee.getId();

        // 首先判断该用户是否被屏蔽
        boolean judge = deliveryService.judgeBlacklist(recruitmentId,employeeId);
        if(judge){
            return new Result("502","sorry,您没有权限");
        }

        // 否则表示没有被屏蔽,进行判断是否可以投递
        // 判断是否可以进行投递
        boolean flag = deliveryService.judgeDelivery(recruitmentId,employeeId);

        // 如果可以投递,则往redis里插数据
        if(flag){
            deliveryService.saveToRedis(resumeId,recruitmentId,employeeId);
            return new Result("200","投递成功");
        }

        // 如果不可以投递,则直接返回不行
        return new Result("501","三个月内仅可以投递一次");
    }

    @PostMapping("/job/count")
    public Result count(Integer rid) {
        recruitmentService.searchFrequency(rid);
        return new Result("200", "次数增加成功");
    }

    @PostMapping("/job/del")
    public Result del(HttpServletRequest request,Integer id) {
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
////        EmployeeCollection employee = new EmployeeCollection();
////        employee.setEmployeeId(4);
//        int count = employeeCollectionService.dropId(id, employee.getId());
////        System.out.println(count);

        boolean flag=employeeCollectionService.judgeCollection(employee.getId(),id);
        if(flag){
            //如果redis收藏,删除redis中的数据
            employeeCollectionService.delToRedis(id,employee.getId());
            employeeCollectionService.dropId(id,employee.getId());
            return new Result("200", "删除成功");
        }else{
            return new Result("500","抱歉,您还未添加");
        }
//        return new Result("200", "删除成功");
    }

    @PostMapping("/job/black")
    public Result black(HttpServletRequest request,Integer id) {
//        System.out.println(id);
        HttpSession session=request.getSession();
        Employee employee= (Employee) session.getAttribute("employee");
//        Employee employee = new Employee();
//        employee.setId(4);
//        Recruitment recruitment = recruitmentService.loadRecruitment(id);
//        System.out.println(recruitment);
        boolean flag = employeeBlackListService.judgeBlacklist(employee.getId(),id);
        if(flag){
            employeeBlackListService.delToRedis(id,employee.getId());
            employeeBlackListService.dropBlack(employee.getId(),id);
            return new Result("200", "取消成功！");
        }else {
            return new Result("500","抱歉,您还未屏蔽");
        }

    }
}
